#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
int main() try {
    sql::mysql::MySQL_Driver* driver = sql::mysql::get_mysql_driver_instance();
    std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", "950330g.G"));
    con->setSchema("mysqltest"); // 设置数据库名
    std::unique_ptr<sql::Statement> stmt(con->createStatement());

    bool flag = stmt->execute("INSERT INTO People VALUES (1, '张三')");
    if (flag) std::cout << "INSERT成功" << std::endl;

    std::unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * from People"));
    while (res->next()) {
        //std::string str = res->getString("name"); // 注意这里的列名应该与查询结果中的列名一致或使用数字索引如 res->getString(1)
        //std::cout << str << std::endl;
        std::cout << res->getString(1) << " " << res->getString(2) << std::endl;
    }
    return 0;
} catch (sql::SQLException &e) {
    std::cerr << "# ERR: SQLException in " << __FILE__;
    std::cerr << "(" << __FUNCTION__ << ") on line " 
              << __LINE__ << std::endl;
    std::cerr << "# ERR: " << e.what();
    std::cerr << " (MySQL error code: " << e.getErrorCode();
    std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    return 1;
}

//g++ main.cpp -o main -I/usr/include/cppconn -L/usr/lib -lmysqlcppconn